package com.idealighter.game.group.manager;

import com.google.inject.Inject;
import com.google.inject.Singleton;

import com.idealighter.game.common.CommonMsgMgr;
import com.idealighter.game.core.assertions.HuohuaAssert;
import com.idealighter.game.core.constant.group.GroupActiveConstant;
import com.idealighter.game.core.constant.group.GroupApplyState;
import com.idealighter.game.core.constant.group.GroupRoleConstant;
import com.idealighter.game.core.error.ErrorCode;
import com.idealighter.game.group.convert.GroupConvert;
import com.idealighter.game.group.dto.GroupInfoDto;
import com.idealighter.game.message.core.ResMessage;
import com.idealighter.game.player.manager.PlayerMgr;
import com.idealighter.game.player.service.bo.PlayerBo;
import com.idealighter.game.player.struct.Player;
import com.idealighter.game.server.db.datadb.dao.RoomCardGroupDao;
import com.idealighter.game.server.db.datadb.dao.RoomCardGroupMemberApplyDao;
import com.idealighter.game.server.db.datadb.dao.RoomCardGroupMemberDao;
import com.idealighter.game.server.db.datadb.dao.RoomCardScoreDao;
import com.idealighter.game.server.db.datadb.generate.domain.RoomCardGroupDomain;
import com.idealighter.game.server.db.datadb.generate.domain.RoomCardGroupMemberApplyDomain;
import com.idealighter.game.server.db.datadb.generate.domain.RoomCardGroupMemberDomain;
import com.idealighter.game.server.db.datadb.mapper.domain.ScoreRangeDomain;
import com.idealighter.utils.check.EmptyUtil;
import com.idealighter.utils.time.TimeUtil;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.mybatis.guice.transactional.Transactional;

@Singleton
public class GroupMgr {

  private static final int GROUP_CREATE_LIMIT = 5;

  private static final int GROUP_JOIN_MAX = 20;

  private static final int GROUP_MAX_PEOPLE_LIMIT = 500;

  private static final int GROUP_MAX_ADMIN_LIMIT = 5;


  private RoomCardGroupDao groupDao;

  private RoomCardGroupMemberDao groupMemberDao;

  private RoomCardGroupMemberApplyDao groupMemberApplyDao;

  private RoomCardScoreDao cardScoreDao;

  @Inject
  private GroupMsgMgr msgMgr;

  @Inject
  private CommonMsgMgr commonMsgMgr;

  @Inject
  private PlayerMgr playerMgr;

  /**
   * 构造函数.
   * 
   * @param groupDao .
   * @param groupMemberDao .
   * @param groupMemberApplyDao .
   */
  @Inject
  public GroupMgr(RoomCardGroupDao groupDao, RoomCardGroupMemberDao groupMemberDao,
      RoomCardGroupMemberApplyDao groupMemberApplyDao, RoomCardScoreDao cardScoreDao) {
    this.groupDao = groupDao;
    this.groupMemberDao = groupMemberDao;
    this.groupMemberApplyDao = groupMemberApplyDao;
    this.cardScoreDao = cardScoreDao;
  }

  private void createGroupCheck(long playerId) {
    long count = groupMemberDao.countMyCreateRoom(playerId);

    HuohuaAssert.isTrue(count < GROUP_CREATE_LIMIT, ErrorCode.GROUP_CREATE_LIMIT);
  }

  private void joinGroupCheck(long playerId) {
    long count = groupMemberDao.countMyJoinRoom(playerId);

    HuohuaAssert.isTrue(count < GROUP_JOIN_MAX, ErrorCode.GROUP_JOIN_LIMIT);
  }

  /**
   * 创建群组 .
   *
   * @author abin
   * @date 2018年5月21日 下午8:08:23
   * @param player 玩家.
   * @param groupName 群.
   * @return 创建结果.
   */
  @Transactional
  public ResMessage createGroup(Player player, String groupName) {
    HuohuaAssert.isTrue(EmptyUtil.stringIsNotEmpty(groupName) && groupName.length() <= 45);

    createGroupCheck(player.getId());

    joinGroupCheck(player.getId());

    Date now = TimeUtil.now();
    RoomCardGroupDomain domain = new RoomCardGroupDomain();
    domain.setCreateTime(now);
    domain.setGroupName(groupName);
    domain.setMaxMember(GROUP_MAX_PEOPLE_LIMIT);
    domain.setMemberNum(1);
    domain.setOwnerId(player.getId());
    domain.setOwnerSuperId(player.getSuperId());
    domain.setOwnerPlayerName(player.getPlayerName());
    domain.setRoomNum(0);
    domain.setState(GroupActiveConstant.ACTIVE);

    int out = groupDao.insertSelective(domain);

    HuohuaAssert.isTrue(out > 0 && domain.getId() > 0, ErrorCode.INTERNAL_SERVER_ERROR);

    RoomCardGroupMemberDomain memberDomain = new RoomCardGroupMemberDomain();
    memberDomain.setGroupId(domain.getId());
    memberDomain.setJoinTime(now);
    memberDomain.setPlayerAvatar(player.getPlayerAvatar());
    memberDomain.setPlayerId(player.getId());
    memberDomain.setPlayerName(player.getPlayerName());
    memberDomain.setRole(GroupRoleConstant.OWNER);
    memberDomain.setPlayerSuperId(player.getSuperId());

    out = groupMemberDao.insertSelective(memberDomain);

    HuohuaAssert.isTrue(out > 0, ErrorCode.INTERNAL_SERVER_ERROR);

    return msgMgr.resCreateGroup(domain.getId());
  }

  /**
   * 查找并申请加群 .
   *
   * @author abin
   * @date 2018年5月21日 下午8:07:49
   * @param player 玩家.
   * @param groupId 群id.
   * @return 结果.
   */
  public ResMessage findAndApplyGroup(Player player, int groupId) {
    HuohuaAssert.isTrue(groupId > 0);

    ResMessage msg = ResMessage.DEFAULT;

    joinGroupCheck(player.getId());

    RoomCardGroupDomain domain = groupDao.selectByPrimaryKey(groupId);
    if (domain != null) {
      HuohuaAssert.isTrue(domain.getMemberNum() <= domain.getMaxMember(),
          ErrorCode.GROUP_MEMBER_LIMIT);
      RoomCardGroupMemberDomain memberDomain = groupMemberDao.selectMyRole(groupId, player.getId());
      HuohuaAssert.isTrue(memberDomain == null, ErrorCode.GROUP_ALREADY_JOIN);

      RoomCardGroupMemberApplyDomain apply =
          groupMemberApplyDao.selectMyApply(groupId, player.getId());


      if (apply != null && apply.getState().equals(GroupApplyState.NOT_DEAL)) {
        HuohuaAssert.isTrue(false, ErrorCode.GROUP_ALREADY_APPLY);
      }


      if (apply != null && apply.getState().equals(GroupApplyState.BLACK)) {
        HuohuaAssert.isTrue(false, ErrorCode.GROUP_ALREADY_REJECT);
      }

      apply = new RoomCardGroupMemberApplyDomain();
      Date now = TimeUtil.now();
      apply.setApplyTime(now);
      apply.setGroupId(groupId);
      apply.setPlayerAvatar(player.getPlayerAvatar());
      apply.setPlayerId(player.getId());
      apply.setPlayerName(player.getPlayerName());
      apply.setPlayerSuperId(player.getSuperId());
      apply.setState(GroupApplyState.NOT_DEAL);

      int out = groupMemberApplyDao.insertSelective(apply);
      HuohuaAssert.isTrue(out > 0, ErrorCode.INTERNAL_SERVER_ERROR);
    } else {
      msg = commonMsgMgr.resErrorMsg(ErrorCode.GROUP_NO_EXIST);
    }
    return msg;
  }

  /**
   * 我的群 .
   *
   * @author abin
   * @date 2018年5月21日 下午8:13:11
   * @param player 玩家.
   * @param lastGroupId 最后群id.
   * @param pageSize 页大小.
   * @return 群列表.
   */
  public ResMessage myGroup(Player player, Integer lastGroupId, int pageSize) {
    HuohuaAssert.isTrue(pageSize > 0 && pageSize <= 20);
    if (lastGroupId != null && lastGroupId <= 0) {
      lastGroupId = null;
    }
    List<Integer> ids = groupMemberDao.selectMyGroup(player.getId(), lastGroupId, pageSize);

    List<GroupInfoDto> dtos = new ArrayList<>();
    List<RoomCardGroupDomain> groupDomains = null;
    if (EmptyUtil.listIsNotEmpty(ids)) {
      groupDomains = groupDao.selectByIdList(ids);
      if (EmptyUtil.listIsNotEmpty(groupDomains)) {
        for (RoomCardGroupDomain groupDomain : groupDomains) {
          List<String> avatars = groupMemberDao.selectTop9Avatar(groupDomain.getId());
          GroupInfoDto dto = GroupConvert.INSTANCE.toGroupInfoDto(groupDomain);
          dto.setMemberAvatars(avatars);
          dtos.add(dto);
        }
      }
    }

    return msgMgr.resMyGroup(dtos);
  }

  /**
   * 牛友群详情 .
   *
   * @author abin
   * @date 2018年5月23日 下午4:05:04
   * @param player 玩家.
   * @param groupId 群id.
   * @return 结果.
   */
  public ResMessage groupDetail(Player player, int groupId) {
    HuohuaAssert.isTrue(groupId > 0);
    RoomCardGroupDomain groupDomain = groupDao.selectByPrimaryKey(groupId);
    HuohuaAssert.notNull(groupDomain, ErrorCode.GROUP_NO_EXIST);

    List<String> avatars = groupMemberDao.selectTop9Avatar(groupDomain.getId());

    GroupInfoDto dto = GroupConvert.INSTANCE.toGroupInfoDto(groupDomain);
    dto.setMemberAvatars(avatars);
    boolean hasMsg = false;
    RoomCardGroupMemberDomain memberDomain = groupMemberDao.selectMyRole(groupId, player.getId());
    Byte role = null;
    if (memberDomain != null && memberDomain.getRole() != null) {
      if (memberDomain.getRole().byteValue() == GroupRoleConstant.ADMIN
          || memberDomain.getRole().byteValue() == GroupRoleConstant.OWNER) {
        hasMsg = groupMemberApplyDao.hasMessage(groupId);
      }
      role = memberDomain.getRole();
    }

    return msgMgr.resGroupDetail(dto, hasMsg, role);
  }


  /**
   * 修改群信息 .
   *
   * @author abin
   * @date 2018年5月23日 下午4:05:49
   * @param player 玩家.
   * @param groupId 群id.
   * @param groupName 群名称.
   * @return 结果.
   */
  public ResMessage modifyGroup(Player player, Integer groupId, String groupName) {
    HuohuaAssert.isTrue(groupId > 0);
    HuohuaAssert.isTrue(EmptyUtil.stringIsNotEmpty(groupName) && groupName.length() <= 45);

    HuohuaAssert.isTrue(groupMemberDao.isOwner(groupId, player.getId()), ErrorCode.GROUP_AUTH_FAIL);

    int out = groupDao.updateName(groupId, player.getId(), groupName);

    HuohuaAssert.isTrue(out > 0, ErrorCode.INTERNAL_SERVER_ERROR);

    return ResMessage.DEFAULT;
  }



  /**
   * 转让 .
   *
   * @author abin
   * @date 2018年5月23日 下午4:12:42
   * @param player 玩家.
   * @param groupId 群id.
   * @param toPlayerId 玩家id.
   * @return 结果.
   */
  @Transactional
  public ResMessage transferGroup(Player player, Integer groupId, Long toPlayerId) {
    HuohuaAssert.isTrue(groupId > 0 && toPlayerId > 0);

    RoomCardGroupDomain groupDomain = groupDao.selectByPrimaryKey(groupId);
    HuohuaAssert.notNull(groupDomain, ErrorCode.GROUP_NO_EXIST);


    HuohuaAssert.isTrue(groupMemberDao.isOwner(groupId, player.getId()), ErrorCode.GROUP_AUTH_FAIL);

    PlayerBo toPlayer = playerMgr.selectPlayer(toPlayerId);

    HuohuaAssert.isTrue(toPlayer != null, ErrorCode.PLAYER_NO_EXIST);

    Long playerId = player.getId();

    int out = groupDao.updateOwner(groupId, playerId, toPlayerId, toPlayer.getSuperId(),
        toPlayer.getPlayerName());

    HuohuaAssert.isTrue(out > 0, ErrorCode.INTERNAL_SERVER_ERROR);

    out = groupMemberDao.changeRole(groupId, playerId, GroupRoleConstant.NORMAL);

    HuohuaAssert.isTrue(out > 0, ErrorCode.INTERNAL_SERVER_ERROR);

    out = groupMemberDao.changeRole(groupId, toPlayerId, GroupRoleConstant.OWNER);

    HuohuaAssert.isTrue(out > 0, ErrorCode.INTERNAL_SERVER_ERROR);

    return ResMessage.DEFAULT;
  }

  /**
   * 任命/解雇 管理员 .
   *
   * @author abin
   * @date 2018年5月23日 下午4:13:15
   * @param player 玩家.
   * @param groupId 群id.
   * @param toPlayerId 玩家id.
   * @param type 类型(0:撤销,1:任命).
   * @return 结果.
   */
  public ResMessage appointRevocation(Player player, int groupId, long toPlayerId, int type) {

    HuohuaAssert.isTrue(groupId > 0 && toPlayerId > 0);

    RoomCardGroupDomain groupDomain = groupDao.selectByPrimaryKey(groupId);
    HuohuaAssert.notNull(groupDomain, ErrorCode.GROUP_NO_EXIST);


    HuohuaAssert.isTrue(type == 0 || type == 1);

    HuohuaAssert.isTrue(groupMemberDao.isOwner(groupId, player.getId()), ErrorCode.GROUP_AUTH_FAIL);

    PlayerBo toPlayer = playerMgr.selectPlayer(toPlayerId);

    HuohuaAssert.isTrue(toPlayer != null, ErrorCode.PLAYER_NO_EXIST);

    Byte role = null;
    if (type == 0) {
      role = GroupRoleConstant.NORMAL;
    } else if (type == 1) {
      role = GroupRoleConstant.ADMIN;
      long num = groupMemberDao.countAdmin(groupId);
      HuohuaAssert.isTrue(num <= GROUP_MAX_ADMIN_LIMIT, ErrorCode.GROUP_ADMIN_LIMIT);
    }

    int out = groupMemberDao.changeRole(groupId, toPlayerId, role);

    HuohuaAssert.isTrue(out > 0, ErrorCode.INTERNAL_SERVER_ERROR);

    return ResMessage.DEFAULT;
  }

  /**
   * 踢出成员 .
   *
   * @author abin
   * @date 2018年5月23日 下午4:14:18
   * @param player 玩家.
   * @param groupId 群id.
   * @param toPlayerId 玩家id.
   * @return 结果.
   */
  public ResMessage kickoutMember(Player player, int groupId, long toPlayerId) {
    HuohuaAssert.isTrue(groupId > 0 && toPlayerId > 0 && player.getId() != toPlayerId);

    RoomCardGroupDomain groupDomain = groupDao.selectByPrimaryKey(groupId);
    HuohuaAssert.notNull(groupDomain, ErrorCode.GROUP_NO_EXIST);


    HuohuaAssert.isTrue(groupMemberDao.isAdminOrOwner(groupId, player.getId()),
        ErrorCode.GROUP_AUTH_FAIL);

    PlayerBo toPlayer = playerMgr.selectPlayer(toPlayerId);

    HuohuaAssert.isTrue(toPlayer != null, ErrorCode.PLAYER_NO_EXIST);



    int out = groupMemberDao.delete(groupId, toPlayerId);

    HuohuaAssert.isTrue(out > 0, ErrorCode.INTERNAL_SERVER_ERROR);

    out = groupDao.decreaseMembers(groupId);

    HuohuaAssert.isTrue(out > 0, ErrorCode.INTERNAL_SERVER_ERROR);

    return ResMessage.DEFAULT;
  }

  /**
   * 申请列表 .
   *
   * @author abin
   * @date 2018年5月23日 下午4:14:43
   * @param player 玩家.
   * @param groupId 群id.
   * @return 结果.
   */
  public ResMessage applyList(Player player, Integer groupId) {
    HuohuaAssert.isTrue(groupId > 0);

    List<RoomCardGroupMemberApplyDomain> applyDomains =
        groupMemberApplyDao.selectApplyList(groupId, GroupApplyState.NOT_DEAL);

    return msgMgr.resApplyList(applyDomains);
  }

  /**
   * 黑名单 列表 .
   *
   * @author abin
   * @date 2018年5月23日 下午4:15:00
   * @param player 玩家.
   * @param groupId 群id.
   * @return 结果.
   */
  public ResMessage blackList(Player player, Integer groupId) {
    HuohuaAssert.isTrue(groupId > 0);

    List<RoomCardGroupMemberApplyDomain> applyDomains =
        groupMemberApplyDao.selectApplyList(groupId, GroupApplyState.BLACK);

    return msgMgr.resBlackList(applyDomains);
  }

  /**
   * 处理请求 .
   *
   * @author abin
   * @date 2018年5月23日 下午4:15:25
   * @param player 玩家id.
   * @param applyId 申请id.
   * @param action 操作类型(0:拒绝,1:同意,2:屏蔽).
   * @return 结果.
   */
  public ResMessage dealApply(Player player, Integer applyId, Integer action) {
    HuohuaAssert.isTrue(applyId > 0);
    HuohuaAssert.isTrue(action >= 0 && action <= 2);

    RoomCardGroupMemberApplyDomain applyDomain = groupMemberApplyDao.selectByPrimaryKey(applyId);
    HuohuaAssert.notNull(applyDomain, ErrorCode.GROUP_APPLY_DEALED);

    Integer groupId = applyDomain.getGroupId();
    HuohuaAssert.isTrue(groupMemberDao.isAdminOrOwner(groupId, player.getId()),
        ErrorCode.GROUP_AUTH_FAIL);

    RoomCardGroupDomain groupDomain = groupDao.selectByPrimaryKey(groupId);
    HuohuaAssert.notNull(groupDomain, ErrorCode.GROUP_NO_EXIST);

    PlayerBo toPlayer = playerMgr.selectPlayer(applyDomain.getPlayerId());

    HuohuaAssert.isTrue(toPlayer != null, ErrorCode.PLAYER_NO_EXIST);

    if (action == 0) {
      // 拒绝 删除
      int out = groupMemberApplyDao.delete(applyId);
      HuohuaAssert.isTrue(out > 0, ErrorCode.INTERNAL_SERVER_ERROR);
    } else if (action == 1) {

      HuohuaAssert.isTrue(groupDomain.getMemberNum() <= groupDomain.getMaxMember(),
          ErrorCode.GROUP_MEMBER_LIMIT);

      Date now = TimeUtil.now();
      RoomCardGroupMemberDomain memberDomain = new RoomCardGroupMemberDomain();
      memberDomain.setGroupId(groupId);
      memberDomain.setJoinTime(now);
      memberDomain.setPlayerAvatar(toPlayer.getPlayerAvatar());
      memberDomain.setPlayerId(toPlayer.getId());
      memberDomain.setPlayerName(toPlayer.getPlayerName());
      memberDomain.setPlayerSuperId(toPlayer.getSuperId());
      memberDomain.setRole(GroupRoleConstant.NORMAL);

      int out = groupMemberDao.insertSelective(memberDomain);

      HuohuaAssert.isTrue(out > 0, ErrorCode.INTERNAL_SERVER_ERROR);

      // 同意现在先删除
      out = groupMemberApplyDao.delete(applyId);
      HuohuaAssert.isTrue(out > 0, ErrorCode.INTERNAL_SERVER_ERROR);

      out = groupDao.increaseMembers(groupId);
      HuohuaAssert.isTrue(out > 0, ErrorCode.INTERNAL_SERVER_ERROR);

    } else if (action == 2) {
      int out = groupMemberApplyDao.updateForDeal(applyId, GroupApplyState.BLACK);
      HuohuaAssert.isTrue(out > 0, ErrorCode.INTERNAL_SERVER_ERROR);
    }


    return ResMessage.DEFAULT;
  }

  /**
   * 群成员列表 .
   *
   * @author abin
   * @date 2018年5月23日 下午4:16:01
   * @param player 玩家.
   * @param groupId 群id.
   * @param lastMemberId 最后的成员id.
   * @param pageSize 页数.
   * @return 结果.
   */
  public ResMessage groupMemberList(Player player, int groupId, int lastMemberId, int lastRole,
      int pageSize) {
    HuohuaAssert.isTrue(groupId > 0 && pageSize > 0 && pageSize <= 20);
    Integer lastMemberIdInt = null;
    if (lastMemberId <= 0) {
      lastMemberIdInt = null;
    } else {
      lastMemberIdInt = lastMemberId;
    }
    Byte role = null;
    if (lastRole < GroupRoleConstant.NORMAL) {
      role = GroupRoleConstant.OWNER;
    } else {
      role = (byte) lastRole;
    }

    HuohuaAssert.isTrue(groupMemberDao.isMember(groupId, player.getId()),
        ErrorCode.GROUP_AUTH_FAIL);

    List<RoomCardGroupMemberDomain> memberDomains =
        groupMemberDao.selectByLimit(groupId, lastMemberIdInt, role, pageSize);

    return msgMgr.resMemberList(memberDomains);
  }

  /**
   * 查找群成员 .
   *
   * @author abin
   * @date 2018年5月24日 下午3:37:45
   * @param player 玩家.
   * @param groupId 群id.
   * @param toPlayerId 目标id.
   * @return 消息.
   */
  public ResMessage findMember(Player player, int groupId, long toPlayerId) {
    HuohuaAssert.isTrue(groupId > 0 && toPlayerId > 0);

    HuohuaAssert.isTrue(groupMemberDao.isMember(groupId, player.getId()),
        ErrorCode.GROUP_AUTH_FAIL);

    RoomCardGroupMemberDomain memberDomain = groupMemberDao.selectMyRole(groupId, toPlayerId);
    ResMessage msg = null;
    if (memberDomain != null) {
      msg = msgMgr.resMemberDetail(memberDomain);
    } else {
      msg = commonMsgMgr.resErrorMsg(ErrorCode.GROUP_MEMBER_NO_FIND);
    }
    return msg;
  }


  private void doDissolveGroup(Integer groupId) {
    int out = groupDao.deleteById(groupId);
    HuohuaAssert.isTrue(out > 0, ErrorCode.INTERNAL_SERVER_ERROR);

    out = groupMemberDao.deleteAllByGroupId(groupId);
    HuohuaAssert.isTrue(out > 0, ErrorCode.INTERNAL_SERVER_ERROR);

    groupMemberApplyDao.deleteAllByGroupId(groupId);
  }

  /**
   * 解散.
   *
   * @author abin
   * @date 2018年5月23日 下午4:06:41
   * @param player 玩家.
   * @param groupId 群id.
   * @return 结果.
   */
  @Transactional
  public ResMessage dissolveGroup(Player player, int groupId) {
    HuohuaAssert.isTrue(groupId > 0);

    RoomCardGroupDomain groupDomain = groupDao.selectByPrimaryKey(groupId);
    HuohuaAssert.notNull(groupDomain, ErrorCode.GROUP_NO_EXIST);


    HuohuaAssert.isTrue(groupMemberDao.isOwner(groupId, player.getId()), ErrorCode.GROUP_AUTH_FAIL);

    doDissolveGroup(groupId);

    return ResMessage.DEFAULT;
  }

  private void doExitGroup(long playerId, int groupId) {
    int out = groupMemberDao.delete(groupId, playerId);

    HuohuaAssert.isTrue(out > 0, ErrorCode.INTERNAL_SERVER_ERROR);

    out = groupDao.decreaseMembers(groupId);

    HuohuaAssert.isTrue(out > 0, ErrorCode.INTERNAL_SERVER_ERROR);

  }

  /**
   * 退出群 .
   *
   * @author abin
   * @date 2018年5月24日 下午5:49:30
   * @param player 玩家id.
   * @param groupId 群id.
   * @return 消息.
   */
  public ResMessage exitGroup(Player player, int groupId) {
    HuohuaAssert.isTrue(groupId > 0);

    Long playerId = player.getId();

    RoomCardGroupDomain groupDomain = groupDao.selectByPrimaryKey(groupId);
    HuohuaAssert.notNull(groupDomain, ErrorCode.GROUP_NO_EXIST);


    HuohuaAssert.isTrue(groupMemberDao.isMember(groupId, playerId), ErrorCode.GROUP_AUTH_FAIL);

    if (groupMemberDao.isOwner(groupId, playerId)) {
      doDissolveGroup(groupId);
    } else {
      doExitGroup(player.getId(), groupId);
    }
    return ResMessage.DEFAULT;
  }

  private static final long RANGE_TIME = 3 * 24 * 60 * 60 * 1000;

  /**
   * 获取牛友群战绩 .
   *
   * @author abin
   * @date 2018年5月25日 下午5:58:06
   * @param player 玩家id.
   * @param lastPlayerId 最后一名玩家id.
   * @param createTime 开始时间.
   * @param endTime 结束时间.
   * @param pageSize 页大小.
   * @return 消息.
   */
  public ResMessage resGroupScore(Player player, Integer groupId, Long lastPlayerId,
      long createTime, long endTime, int pageSize) {
    HuohuaAssert.isTrue(createTime > 0 && endTime > 0 && pageSize > 0 && groupId > 0);

    Date createTimeDate = new Date(createTime);
    Date endTimeDate = new Date(endTime);

    HuohuaAssert.isTrue(endTimeDate.after(createTimeDate) && endTime - createTime <= RANGE_TIME);

    if (lastPlayerId != null && lastPlayerId <= 0) {
      lastPlayerId = null;
    }

    List<ScoreRangeDomain> scoreRangeDomains =
        cardScoreDao.selectScoreRange(groupId, lastPlayerId, createTimeDate, endTimeDate, pageSize);


    return msgMgr.resGroupScore(scoreRangeDomains);
  }
}
